
import json
import sys
from pyecharts.charts import Timeline, EffectScatter, Grid, WordCloud

from pyecharts import options as opts



def create_bamboo_timeline():
    with open("三国事件表.json", "r", encoding="utf-8") as f:
        data = json.load(f)
    timeline = Timeline(
        init_opts=opts.InitOpts(
            # width="1200px",
                                # height="750px"
                                )
    )
    years = data.keys()

    for year in years:
        word = WordCloud(
            # init_opts=opts.InitOpts(width="600px", height="400px")
        )

        ls = [i.split("，") for i in data[year]]
        word_li = []
        for i in ls:
            if type(i) == list:
                for j in i:
                    word_li.append(j)
            else:
                word_li.append(i)

        word_data = [(i, 2) for i in word_li]

        word.add("三国演义事件", word_data,
                 word_size_range=[6, 66],
                 # shape="circle",
                 rotate_step=2,
                 emphasis_shadow_blur=10,

                 )

        word.set_global_opts(title_opts=opts.TitleOpts(title="三国演义时间轴"))


        timeline.add(word, f"{year}年")
        timeline.add_schema(width="800", play_interval=300)

    return timeline

create_bamboo_timeline().render("三国演义-时间轴.html")